/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JTimeStoreEditor.java
 *
 * Created on 2009-11-10, 22:30:45
 */

package cn.ekuma.epos.inventory;

import cn.ekuma.data.ui.swing.AbstractDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.db.table.erp.I_TimeStoreLine_V;
import cn.ekuma.epos.inventory.model.TimeStoreLineTableModel;
import cn.ekuma.util.SwingUtil;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.bean.Location;
import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.erp.TimeStore;
import com.openbravo.bean.erp.viewbean.TimeStoreLine_V;
import java.awt.Component;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;

/**
 *
 * @author Administrator
 */
public class JTimeStoreEditor extends AbstractDAOJEditor<TimeStore>{
   I_DataLogicERP m_dlSales;
    private final DirtyManager dirty;
   TimeStoreLineTableModel linesTableModel;

   private ComboBoxValModel m_LocationModel;

   private ComboBoxValModel m_CategoryModel1;
   private Boolean isBuild;
   
   QBFParameters productSearchQBF,searchQBF;
   QBFParameter code,ref,nameP,origin,mnemonic,timeStoreId;


    public JTimeStoreEditor(I_DataLogicERP dlSales,DirtyManager dirty) {
       super(dirty);
       initComponents();
       this.dirty=dirty;
       this.m_dlSales=dlSales;
       linesTableModel=new TimeStoreLineTableModel();
       jXTable1.setModel(linesTableModel);
       jPanel7 = new javax.swing.JPanel();
       jPanel2.add(jPanel7, BorderLayout.NORTH);
       jButton4 = new javax.swing.JButton();
       
               jButton4.setIcon(new ImageIcon(JTimeStoreEditor.class.getResource("/com/openbravo/images/button_ok.png")));
               jButton4.setFocusPainted(false);
               jButton4.setFocusable(false);
               jButton4.setRequestFocusEnabled(false);
               jButton4.addActionListener(new java.awt.event.ActionListener() {
                   public void actionPerformed(java.awt.event.ActionEvent evt) {
                       jButton4ActionPerformed(evt);
                   }
               });
               
               inputCode = new JTextField();
               inputCode.setColumns(10);
               
                       javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
                       jPanel7Layout.setHorizontalGroup(
                       	jPanel7Layout.createParallelGroup(Alignment.TRAILING)
                       		.addGroup(Alignment.LEADING, jPanel7Layout.createSequentialGroup()
                       			.addGap(98)
                       			.addComponent(inputCode, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
                       			.addPreferredGap(ComponentPlacement.RELATED)
                       			.addComponent(jButton4)
                       			.addContainerGap(288, Short.MAX_VALUE))
                       );
                       jPanel7Layout.setVerticalGroup(
                       	jPanel7Layout.createParallelGroup(Alignment.LEADING)
                       		.addGroup(jPanel7Layout.createSequentialGroup()
                       			.addContainerGap()
                       			.addGroup(jPanel7Layout.createParallelGroup(Alignment.BASELINE)
                       				.addComponent(jButton4)
                       				.addComponent(inputCode, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                       			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                       );
                       jPanel7.setLayout(jPanel7Layout);

       jName.getDocument().addDocumentListener(dirty);
       m_jLocation.addActionListener(dirty);
       m_jCategory1.addActionListener(dirty);

        m_CategoryModel1 = new ComboBoxValModel();

      
        m_LocationModel= new ComboBoxValModel();
        
        code=new QBFParameter(I_TimeStoreLine_V.P_CODE);
		ref=new QBFParameter(I_TimeStoreLine_V.P_REFERENCE);
		nameP=new QBFParameter(I_TimeStoreLine_V.P_NAME);
		origin=new QBFParameter(I_TimeStoreLine_V.P_ORIGIN);
		mnemonic=new QBFParameter(I_TimeStoreLine_V.P_MNEMONIC);
		
		productSearchQBF=new QBFParameters();
		productSearchQBF.or(code).or(ref).or(nameP).or(origin).or(mnemonic);
		
		timeStoreId=new QBFParameter(I_TimeStoreLine_V.T_TIMESTOREID);
		searchQBF=new QBFParameters();
		searchQBF.and(timeStoreId).and(productSearchQBF);
		
		inputCode.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				executeSearch();
			}
    	});
    }

    public void activate() throws BasicException {
        m_LocationModel= new ComboBoxValModel(m_dlSales.list(Location.class));
        m_jLocation.setModel(m_LocationModel);
        m_jLocation.setSelectedIndex(0);
        List categerys= m_dlSales.list(ProductCategory.class);
        m_CategoryModel1 = new ComboBoxValModel(categerys);
        m_CategoryModel1.add(0, null);
        m_jCategory1.setModel(m_CategoryModel1);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jCreateTime = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jCreateLines = new javax.swing.JButton();
        jDeleteLines = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        m_jCategory1 = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        m_jLocation = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jName = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setLayout(new java.awt.BorderLayout());

        jLabel3.setText(AppLocal.getIntString("label.timestore.creattime")); // NOI18N

        jCreateTime.setEditable(false);

        jCreateLines.setText(AppLocal.getIntString("Buttom.ordertype.buildlines")); // NOI18N
        jCreateLines.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCreateLinesActionPerformed(evt);
            }
        });

        jDeleteLines.setText(AppLocal.getIntString("buttom.timestore.deletelines")); // NOI18N
        jDeleteLines.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jDeleteLinesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addComponent(jCreateLines)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jDeleteLines)
                .addGap(46, 46, 46))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCreateLines)
                    .addComponent(jDeleteLines))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel8.setText(AppLocal.getIntString("label.prodcategory")); // NOI18N

        jLabel9.setText(AppLocal.getIntString("label.warehouse")); // NOI18N

        jLabel1.setText(AppLocal.getIntString("label.timestore.name")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jName, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(m_jCategory1, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(m_jLocation, javax.swing.GroupLayout.Alignment.LEADING, 0, 200, Short.MAX_VALUE))
                            .addComponent(jCreateTime, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3, jLabel8, jLabel9});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(m_jLocation, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(m_jCategory1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jCreateTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(324, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(AppLocal.getIntString("label.timestore.tabname"), jPanel1); // NOI18N

        jPanel2.setLayout(new java.awt.BorderLayout());

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jXTable1);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab(AppLocal.getIntString("label.timestore.searchTabName"), jPanel2); // NOI18N

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        executeSearch();
}//GEN-LAST:event_jButton4ActionPerformed

    private void jCreateLinesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCreateLinesActionPerformed
        try {
            m_dlSales.createTimeStoreLines(obj);
            jCreateLines.setEnabled(false);
            jDeleteLines.setEnabled(true);
            jTabbedPane1.setEnabledAt(1, isBuild);
            jCreateTime.setText(Formats.TIMESTAMP.formatValue(new Date()));
            obj.setIsBuiled(true);
            dirty.setDirty(true);
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
            obj.setIsBuiled(false);
        }
    }//GEN-LAST:event_jCreateLinesActionPerformed

    private void jDeleteLinesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jDeleteLinesActionPerformed
        try {
            m_dlSales.deleteTimeStoreLines(obj);
            jCreateLines.setEnabled(true);
            jDeleteLines.setEnabled(false);
            jTabbedPane1.setEnabledAt(1, isBuild);
            dirty.setDirty(true);
            obj.setIsBuiled(false);
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
            obj.setIsBuiled(true);
        }
    }//GEN-LAST:event_jDeleteLinesActionPerformed
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jCreateLines;
    private javax.swing.JTextField jCreateTime;
    private javax.swing.JButton jDeleteLines;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jName;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    private javax.swing.JComboBox m_jCategory1;
    private javax.swing.JComboBox m_jLocation;
    private JTextField inputCode;
    // End of variables declaration//GEN-END:variables


    @Override
    public void refresh() {
    }

    @Override
    public Component getComponent() {
        return this;
    }

    private void executeSearch() {
    	String sCode=inputCode.getText();
    	timeStoreId.equal(obj.getID());
    	if(sCode.isEmpty()){
    		try {
    		linesTableModel.resert(m_dlSales.listByFilter(TimeStoreLine_V.class, timeStoreId));
            SwingUtil.PreferredJXTableColumn(jXTable1);
    		} catch (BasicException ex) {
                MessageInf msg = new MessageInf(ex);
                msg.show(this);
                linesTableModel.resert(null);
            }
    		return ;
         }
    	code.equal(sCode);
 		ref.equal(sCode);
 		nameP.link(sCode);
 		origin.link(sCode);
 		mnemonic.link(sCode);
        try {
            linesTableModel.resert(m_dlSales.listByFilter(TimeStoreLine_V.class, searchQBF));
            SwingUtil.PreferredJXTableColumn(jXTable1);
        } catch (BasicException ex) {
            MessageInf msg = new MessageInf(ex);
            msg.show(this);
            linesTableModel.resert(null);
        }
    }

	@Override
	public TimeStore createNew(Properties prop) {	
		return new TimeStore();
	}

	@Override
	public void rebuild(TimeStore obj) throws BasicException {
		obj.setName(jName.getText());
        try {
        	obj.setCreateDate((Date) Formats.TIMESTAMP.parseValue(jCreateTime.getText()));
        } catch (BasicException ex) {
        	obj.setCreateDate(new Date());
        }
        obj.setLocationRef((String) m_LocationModel.getSelectedKey());
        obj.setProductCatRef((String) m_CategoryModel1.getSelectedKey());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		   jName.setEnabled(editAble);
	       m_jLocation.setEnabled(editAble);
	       m_jCategory1.setEnabled(editAble);
	       jDeleteLines.setEnabled(editAble);
	       jPanel2.setEnabled(editAble);
	       jName.setText(obj.getName());
	       jCreateTime.setText(Formats.TIMESTAMP.formatValue(obj.getCreateDate()));
	       m_LocationModel.setSelectedKey(obj.getLocationRef());
	       m_CategoryModel1.setSelectedKey(obj.getProductCatRef());
	       isBuild=obj.isIsBuiled();
	       jCreateLines.setEnabled(false);
	       if(obj.isIsBuiled()){
	            m_jLocation.setEnabled(false);
	            m_jCategory1.setEnabled(false);
	       }else{
	    	   if(state==STATE_Edit){
	               jCreateLines.setEnabled(true);
	    	   }else
	               jDeleteLines.setEnabled(false);
	       }
	       jTabbedPane1.setEnabledAt(1, isBuild);
	}

	@Override
	public Class getEditClass() {
		return TimeStore.class;
	}
}
